/**
 * 
 */
package com.gf.pf.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.common.BaseController;
import com.gf.common.ResultMessage;
import com.gf.common.UUIDGenerator;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.model.Resource;
import com.gf.pf.model.Ztree;
import com.gf.pf.service.MyBatisService;
import com.gf.pf.service.ResourceService;

/**
 * @description 资源管理
 * @date   Mar 7, 2015
 */
@Controller
@RequestMapping("/pf/resource")
public class ResourceController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ResourceController.class);
	
	/**
	 * 资源列表
	 */
	private String RESOURCE_LIST = "pf/resourceList";
	
	/**
	 * 添加资源
	 */
	private String RESOURCE_ADD = "pf/resourceAdd";
	
	/**
	 * 修改资源
	 */
	private String RESOURCE_MODIFY = "pf/resourceModify";
	
	/**
	 * 每页记录数
	 */
	private String PAGE_SQL_ID = "pf.resource.resourceList";
	
	/**
	 * 总记录数
	 */
	private String TOTAL_SQL_ID = "pf.resource.total";
	
	/**
	 * 父资源ID为空！
	 */
	private String parent_resource_id_empty = "父资源ID为空！";
	
	/**
	 * 资源ID为空！
	 */
	private String resource_id_empty = "资源ID为空！";
	
	@Autowired
	private ResourceService resourceService;

	@Autowired
	private MyBatisService myBatisService;
	
	/**
	 * 
	 * @description 资源管理列表
	 * @return String
	 */
	@RequestMapping(value = "/flResource", method = RequestMethod.GET)
	public String resourceList() {
		return this.RESOURCE_LIST;
	}
	
	/**
	 * 
	 * @description 资源管理列表数据
	 * @return Flexigrid
	 */
	@RequestMapping(value = "/dataGrid", method = RequestMethod.POST)
	@ResponseBody
	public Flexigrid dataGrid(Resource resource, Flexigrid flex) {
		if(StringUtils.isEmpty(resource.getId())) {
			resource.setId(super.ROOT_ID);
		}
		flex.setObj(resource);
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList(this.PAGE_SQL_ID, flex);
		int total = myBatisService.count(this.TOTAL_SQL_ID, flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
	
	/**
	 * 
	 * @description 资源树形数据
	 * @return List<Ztree>
	 */
	@RequestMapping(value = "/ztree", method = RequestMethod.POST)
	@ResponseBody
	public List<Ztree> getResourceZtree() {
		return resourceService.getResouceZtree();
	}
	
	/**
	 * 
	 * @description 添加资源跳转
	 * @return String
	 */
	@RequestMapping(value = "/faResource", method = RequestMethod.GET)
	public String resourceAddForward(String pId, Model model) {
		if(!StringUtils.isEmpty(pId)) {
			Resource resource = resourceService.getResource(pId);
			model.addAttribute("resource", resource);
		} else {
			LOGGER.error(this.parent_resource_id_empty);
			return this.RESOURCE_LIST;
		}
		return this.RESOURCE_ADD;
	}
	
	/**
	 * 
	 * @description 添加资源
	 * @return String
	 */
	@RequestMapping(value = "/aResource", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage resourceAdd(Resource resource) {
		if(StringUtils.isEmpty(resource.getResourceParentId())) {
			LOGGER.error(this.parent_resource_id_empty);
		}
		
		resource.setId(UUIDGenerator.getInstance().getUUID32());
		Integer result = resourceService.saveResource(resource);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("资源保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("资源保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 资源修改跳转
	 * @return String
	 */
	@RequestMapping(value = "/fmResource", method = RequestMethod.GET)
	public String resourceModifyForward(String resourceId, Model model) {
		if(!StringUtils.isEmpty(resourceId)) {
			Resource resource = resourceService.getResource(resourceId);
			model.addAttribute("resource", resource);
		} else {
			LOGGER.error(this.resource_id_empty);
			return this.RESOURCE_LIST;
		}
		return this.RESOURCE_MODIFY;
	}
	
	/**
	 * 
	 * @description 资源修改
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/mResource", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage resourceModify(Resource resource) {
		if(StringUtils.isEmpty(resource.getId())) {
			LOGGER.error(this.resource_id_empty);
		}
		
		Integer result = resourceService.modifyResource(resource);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("资源修改成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("资源修改失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 删除资源
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/dResource", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage resourceDelete(String resourceId) {
		if(StringUtils.isEmpty(resourceId)) {
			LOGGER.error(this.resource_id_empty);
		}
		
		Integer result = resourceService.deleteResource(resourceId);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("资源删除成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("资源删除失败！");
		}
		return super.resultMessage;
	}
	
}
